package com.canbot.u05.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.canbot.u05.R;
import com.canbot.u05.bean.AudioInfo;

public class PhotoItem extends RelativeLayout /*implements OnClickListener,OnCheckedChangeListener*/ {

        //显示数字和类型
        private TextView tvNum, tvMimeType;

        //private ImageView ivPhoto;
        private CheckBox cbox;

        private onCheckBoxCheckedListener onBoxCheckedListener;

        private AudioInfo photo;

        private boolean isCheckAll;

        private onGridViewItemClickListener l;

        private int position;

        public PhotoItem(Context context) {
                super(context);
        }

        public PhotoItem(Context context, onCheckBoxCheckedListener onBoxCheckedListener) {
                super(context);
                LayoutInflater.from(context).inflate(R.layout.layout_photoitem, this, true);
                this.onBoxCheckedListener = onBoxCheckedListener;

                // setOnClickListener(this);
                //ivPhoto=(ImageView) findViewById(R.id.iv_photo);
                cbox = (CheckBox) findViewById(R.id.cb_photo_lpsi);
                tvNum = (TextView) findViewById(R.id.tv_num);
                tvMimeType = (TextView) findViewById(R.id.tv_mimeType);
                // CheckBox选中状态改变监听
                //cbox.setOnCheckedChangeListener(this);
        }

        /**
         * chexkBox选中事件监听器
         */
        public interface onCheckBoxCheckedListener {

                void onCheckedChanged(AudioInfo photoModel, CompoundButton buttonView, boolean isChecked);
        }

        /**
         * 图片点击事件
         */
        public interface onGridViewItemClickListener {

                void onItemClick(int position);
        }

        //    @Override
//    public void onClick(View v) {
//        if (l!=null) {
//            l.onItemClick(position);
//        }
//    }
        public void setOnClickListener(onGridViewItemClickListener l, int position) {
                this.l = l;
                this.position = position;
        }

        public CheckBox getCheckBox() {
                return cbox;
        }

        @Override
        public void setSelected(boolean selected) {
                if (photo == null) {
                        return;
                }
                isCheckAll = true;
                cbox.setChecked(selected);
                isCheckAll = false;
        }

        //    @Override
//    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//        if (!isCheckAll) {
//            onBoxCheckedListener.onCheckedChanged(photo, buttonView, isChecked); // 调用主界面回调函数
//        }
//        // 让图片变暗或者变亮
//        if (isChecked) {
//            setDrawingable();
//            ivPhoto.setColorFilter(Color.GRAY, Mode.MULTIPLY);
//        } else {
//            ivPhoto.clearColorFilter();
//        }
//        photo.setChecked(isChecked);
//        
//    }
        public void setNum(String num) {
                tvNum.setText(num);
        }

        /**
         * 显示对应的音频类型
         */
        public void setMimeType(final AudioInfo photo) {
                this.photo = photo;
                //根据媒体类型显示相应图片
                String mimeType = photo.getMimeType();
                Log.i("info", mimeType);
                if (mimeType.contains("mpeg")) {
                        tvMimeType.setText("MP3");
                }
                else if (mimeType.contains("x-ms-wmv")) {
                        tvMimeType.setText("WMV");
                }
                else if (mimeType.contains("x-ms-wma")) {
                        tvMimeType.setText("WMA");
                }
                else if (mimeType.contains("x-wav")) {
                        tvMimeType.setText("WAV");
                }
                else if (mimeType.contains("x-pn-realaudio")) {
                        tvMimeType.setText("RMVB");
                }
                else if (mimeType.contains("ogg")) {
                        tvMimeType.setText("OGG");
                }
        }
}
